package com.project.frame.common.utils;



import java.util.*;

/**
 * json 与 bean的转换
 * super_dev_007
 * 2019/1/14
 */
public class CommonJsonUtils<T> {

    /**
     * json转为List<T>
     * @author super_dev_001
     * @date 2020/1/19
     * @param [json, clazz]
     * @return java.util.List<T>
     */
    public List<T> toBeanList(String json,Class<T> clazz){
        //调用DateMorpherEx，defaultValue为null
//        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
        //com.alibaba.fastjson.JSONObject pObj =  com.alibaba.fastjson.JSON.parseObject(json);
        com.alibaba.fastjson.JSONArray jsonArr = com.alibaba.fastjson.JSONArray.parseArray(json);
        List<T> list = new ArrayList<T>();
        for (int i = 0; i < jsonArr.size(); i++) {
            com.alibaba.fastjson.JSONObject obj = jsonArr.getJSONObject(i);
            T rec = com.alibaba.fastjson.JSON.parseObject(obj.toJSONString(),clazz);
            list.add(rec);
        }
        return list;
    }

    /**
     * json 里包含这 key 的子数组，转换为List<T>
     * @author super_dev_001
     * @date 2020/1/19
     * @param [json, key, clazz]
     * @return java.util.List<T>
     */
    public List<T> toBeanListByKey(String json,String key,Class<T> clazz){
        //调用DateMorpherEx，defaultValue为null
//        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
        List<Map<String,Object>> mapList = getMapListByKey(json,key);
        com.alibaba.fastjson.JSONArray jsonArr = com.alibaba.fastjson.JSONArray.parseArray(com.alibaba.fastjson.JSON.toJSONString(mapList));
        List<T> list = new ArrayList<T>();
        for (int i = 0; i < jsonArr.size(); i++) {
            com.alibaba.fastjson.JSONObject obj = jsonArr.getJSONObject(i);
            T rec = obj.toJavaObject(clazz);
            list.add(rec);
        }
        return list;
    }

    /**
     * 将json转为List<Map<String,Object>>
     * @author super_dev_001
     * @date 2020/1/19
     * @param [json]
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> toMapList(String json){
        com.alibaba.fastjson.JSONArray jsonArr = com.alibaba.fastjson.JSONArray.parseArray(json);
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for (int i = 0; i < jsonArr.size(); i++) {
            com.alibaba.fastjson.JSONObject obj = jsonArr.getJSONObject(i);
            Iterator iterable= obj.entrySet().iterator();
            Map<String,Object> map = new HashMap<String,Object>();
            while(iterable.hasNext()){
                Object key = iterable.next();
                map.put(""+key,obj.get(key));
            }
            list.add(map);
        }
        return list;
    }

    /**
     * json 里包含这 key 的子数组，转换为List<Map<String,Object>>
     * @author super_dev_001
     * @date 2020/1/19
     * @param [json, key]
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> getMapListByKey(String json,String key){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        com.alibaba.fastjson.JSONObject params = com.alibaba.fastjson.JSON.parseObject(json);
        Object objList = params.get(key);
        list = toMapList(objList.toString());
        return list;
    }

    /**
     * 获取json的 指定key 的 Map<String,Object>
     * @author super_dev_001
     * @date 2020/1/19
     * @param [json, key]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> getMapByKey(String json,String key){
        Map<String,Object> map = new HashMap<String,Object>();
        com.alibaba.fastjson.JSONObject params = com.alibaba.fastjson.JSON.parseObject(json);
        Object objMap = params.get(key);
        map = com.alibaba.fastjson.JSON.parseObject(com.alibaba.fastjson.JSON.toJSONString(objMap));
        return map;
    }


    /**
     * 获取json的 指定key 的 Map<String,Object>,并转为 T
     * @author super_dev_001
     * @date 2020/1/19
     * @param [json, key, clazz]
     * @return T
     */
    public T toBeanByKey(String json, String key, Class<T> clazz) {
//        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
        Map<String,Object> map  = getMapByKey(json,key);
        T rec = com.alibaba.fastjson.JSON.parseObject(com.alibaba.fastjson.JSON.toJSONString(map),clazz);
        return rec;
    }

    /**
     *  获取json的 指定key 的 json string
     * @author super_dev_001
     * @date 2020/1/19
     * @param [json, key]
     * @return java.lang.String
     */
    public String getStringByKey(String json,String key){
        com.alibaba.fastjson.JSONObject params = com.alibaba.fastjson.JSON.parseObject(json);
        Object result = params.get(key);
        return ""+result;
    }

    public T toBean(String json, Class<T> clazz){
//        JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpherEx(new String[] { "yyyy-MM-dd HH:mm:ss","yyyy-MM-dd" }, (Date) null));
        T rec = com.alibaba.fastjson.JSON.parseObject(json,clazz);
        return rec;
    }

}
